package com.lframework.xingyun.basedata.controller;

import com.lframework.starter.web.core.annotations.security.HasPermission;
import com.lframework.starter.web.core.controller.DefaultBaseController;
import com.lframework.starter.web.core.components.resp.InvokeResult;
import javax.validation.constraints.NotBlank;

import com.lframework.xingyun.basedata.bo.product.spec.GetProductSpecBo;
import com.lframework.xingyun.basedata.bo.product.spec.QueryProductSpecBo;
import com.lframework.xingyun.basedata.entity.ProductSpec;
import com.lframework.xingyun.basedata.service.product.ProductSpecService;
import com.lframework.xingyun.basedata.vo.product.spec.ProductSpecVo;
import com.lframework.xingyun.basedata.vo.product.spec.QueryProductSpecVo;
import io.swagger.annotations.ApiImplicitParam;
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import io.swagger.annotations.ApiOperation;
import com.lframework.starter.common.utils.CollectionUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品规格表 Controller
 *
 */
@Api(tags = "商品规格表")
@Validated
@RestController
@RequestMapping("/basedata/product/spec")
public class ProductSpecController extends DefaultBaseController {

    @Autowired
    private ProductSpecService productSpecService;

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @HasPermission({"base-data:product:spec:query"})
    @GetMapping("/query")
    public InvokeResult<List<QueryProductSpecBo>> query(@Valid QueryProductSpecVo vo) {

        List<ProductSpec> datas = productSpecService.query(vo);
        List<QueryProductSpecBo> results = null;

        if (!CollectionUtil.isEmpty(datas)) {
            results = datas.stream().map(QueryProductSpecBo::new).collect(Collectors.toList());
        }

        return InvokeResultBuilder.success(results);
    }

    /**
     * 根据ID查询
     */
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
    @HasPermission({"base-data:product:spec:query"})
    @GetMapping
    public InvokeResult<GetProductSpecBo> get(@NotBlank(message = "id不能为空！") String id) {

        ProductSpec data = productSpecService.findById(id);
        if (data == null) {
            throw new DefaultClientException("商品规格表不存在！");
        }

        GetProductSpecBo result = new GetProductSpecBo(data);

        return InvokeResultBuilder.success(result);
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @HasPermission({"base-data:product:spec:add"})
    @PostMapping("{productId}")
    public InvokeResult<Void> save(@PathVariable String productId, @Valid @RequestBody List<ProductSpecVo> vo) {

        productSpecService.save(productId, vo);

        return InvokeResultBuilder.success();
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @HasPermission({"base-data:product:spec:add"})
    @PostMapping("save/{productId}")
    public InvokeResult<Void> saveSpec(@PathVariable String productId, @Valid @RequestBody List<ProductSpecVo> vo) {

        productSpecService.save(productId, vo);

        return InvokeResultBuilder.success();
    }
}

